// pages/myorder/myorder.js
var api = require('../../config/api.js');
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeTag: 0,
    page: 1,
    limit: 10,
    list: [],
    totalPages: 1,
    dealType: 0,
    showPayType: false,
  },
  //获取数据
  getData() {
    let that = this;
    util.request(api.OrderList, {
      showType: that.data.dealType,
      page: that.data.page,
      limit: that.data.limit
    }).then(function(res) {
      if (res.errno == 0) {
        that.setData({
          list: that.data.list.concat(res.data.list),
          totalPages: res.data.pages
        })
      } else {
        util.myToast(res.errmsg)
      }
    });
  },
  //获取数据没有加载框
  getNoLoadingData() {
    let that = this;
    util.noLoadingRequest(api.OrderList, {
      showType: that.data.dealType,
      page: that.data.page,
      limit: that.data.limit
    }).then(function(res) {
      if (res.errno == 0) {
        that.setData({
          list: that.data.list.concat(res.data.list),
          totalPages: res.data.pages
        })
      } else {
        util.myToast(res.errmsg)
      }
    });
  },
  //tab
  onChange(e) {
    var tab = e.detail.index;
    this.setData({
      dealType: tab,
      page: 1,
      list: [],
      totalPages: 1
    })
    this.getData();
  },
  //删除订单
  onDelete(e) {
    var that = this;
    var orderId = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '提示',
      content: '确定删除订单？',
      success(res) {
        if (res.confirm) {
          that.getDeleteData(orderId, index);
        }
      }
    })
  },

  //关闭弹窗
  onClose(e) {
    this.setData({
      showPayType: false
    })
  },
  //立即付款
  onPay(e) {
    var orderId = e.currentTarget.dataset.id;
    var orderSn = e.currentTarget.dataset.ordersn;

    this.setData({
      showPayType: true,
      orderSn: orderSn
    })
    this.getUserData();
  },
  //获取用户数据
  getUserData() {
    let that = this;
    util.request(api.Center).then(function(res) {
      if (res.errno == 0) {
        that.setData({
          balance: res.data.user.balance
        })

      } else {
        util.myToast(res.errmsg)
      }
    });
  },
  //确认收货
  onConfirm(e) {
    var that = this;
    var orderId = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确认收货将无法退款!',
      success(res) {
        if (res.confirm) {

          that.getConfirmData(orderId);
        }
      }
    })
  },
  //去评价
  onComment(e) {
    var orderId = e.currentTarget.dataset.id;
    util.toClass('/pages/submit-evaluate/submit-evaluate', {
      orderid: orderId
    })
  },
  //申请退款
  onRefund(e) {
    var orderId = e.currentTarget.dataset.id;
    util.toClass('/pages/apply-refund/apply-refund', {
      orderid: orderId
    })
  },
  //取消订单
  onCancle(e) {
    var that = this;
    var orderId = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定取消订单？',
      success(res) {
        if (res.confirm) {

          that.getCancleData(orderId);
        }
      }
    })
  },
  //再来一单
  onReBuy(e) {
    var orderId = e.currentTarget.dataset.id;
  },
  //查看物流
  onWuliu(e) {
    util.myToast('暂无物流信息');
  },
  //商品详情
  onItemClick(e) {
    var orderId = e.currentTarget.dataset.id;
    var stauts = e.currentTarget.dataset.stauts;
    util.toClass('/pages/details-order/details-order', {
      orderid: orderId
    })

  },
  //删除订单接口
  getDeleteData(orderid, index) {
    let that = this;
    util.request(api.OrderDelete, {
      orderId: orderid
    }, 'post').then(function(res) {
      if (res.errno == 0) {
        var list = that.data.list;
        list.splice(index, 1);
        that.setData({
          list: list
        })
      } else {
        util.myToast(res.errmsg)
      }
    });
  },
  //取消订单接口
  getCancleData(orderid) {
    let that = this;
    util.request(api.OrderCancel, {
      orderId: orderid
    }, 'post').then(function(res) {
      if (res.errno == 0) {
        that.setData({
          page: 1,
          list: [],
          totalPages: 1
        })
        that.getData();
      } else {
        util.myToast(res.errmsg)
      }
    });
  },
  //确认收货接口
  getConfirmData(orderid) {
    let that = this;
    util.request(api.OrderConfirm, {
      orderId: orderid
    }, 'post').then(function(res) {
      if (res.errno == 0) {
        that.setData({
          page: 1,
          list: [],
          totalPages: 1
        })
        that.getData();
      } else {
        util.myToast(res.errmsg)
      }
    });
  },
  //选择余额支付
  onChooseYue(e) {
    this.setData({
      showPayType: false
    })
    this.getBanlenceData();
  },
  //选择微信支付
  onChooseWechat(e) {
    this.setData({
      showPayType: false
    })
    this.onWeChatPay();
  },
  //微信支付
  onWeChatPay: function() {
    let that = this;
    wx.login({
      success: res => {
        var code = res.code;
        util.request(api.StartPay, {
          merReqNo: that.data.orderSn,
          code: code
        }, 'POST').then(function(res) {
          if (res.respCode == 'ZB0000') {
            var payParam = res;
            wx.requestPayment({
              'timeStamp': payParam.timestamp,
              'nonceStr': payParam.noncestr,
              'package': payParam.package,
              'signType': payParam.signtype,
              'paySign': payParam.paysign,
              'success': function(res) {
                //支付成功
                wx.redirectTo({
                  url: '/pages/success-pay/success-pay',
                })

              },
              'fail': function(res) {
                //支付失败
                util.myToast(res.respDesc);
                setTimeout(function(e) {
                  that.setData({
                    page: 1,
                    limit: 10,
                    list: [],
                    totalPages: 1
                  })
                  that.getData();
                }, 2000)
              },
            });
          } else {
            console.log("支付成功-----" + res)
            if (util.isEmpy(res.respDesc)) {
              util.myToast(res.errmsg);
              setTimeout(function(e) {
                that.setData({
                  page: 1,
                  limit: 10,
                  list: [],
                  totalPages: 1
                })
                that.getData();
              }, 2000)

            } else {
              util.myToast(res.respDesc);
            }


          }
        });

      }
    })

  },
  //余额支付
  getBanlenceData() {
    let that = this;

    util.request(api.BalancePay, {
      merReqNo: that.data.orderSn
    }, 'post').then(function(res) {
      if (res.errno == 0) {
        //支付成功
        wx.redirectTo({
          url: '/pages/success-pay/success-pay',
        })
      } else {

        util.myToast(res.errmsg)
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var status = options.status;

    this.setData({
      activeTag: status,
      dealType: status
    })
    this.getData();
  },
  //更新数据方法
  changeData: function() {
    this.setData({
      page: 1,
      limit: 10,
      list: [],
      totalPages: 1
    })
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.totalPages > this.data.page) {
      this.setData({
        page: this.data.page + 1
      });
      this.getData()
    } else {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})